#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include <memory>
#include <cstring>
#include <unistd.h>

#include "InetAddr.hpp"
#include "Log.hpp"
#include "ErrorNum.hpp"

using namespace log_ns;

class Socket;
using SockSPtr = std::shared_ptr<Socket>;

static const int gblacklog = 8;

class Socket
{
public:
    virtual void CreateSocketOrDie() = 0;
    virtual int CreateBindOrDie(uint16_t port) = 0;
    virtual void CreateListenOrDie(int backlog = gblacklog) = 0;
    virtual int Accept(InetAddr *cliaddr) = 0;
    virtual bool Connect(const std::string &peerip, const uint16_t peerport) = 0;
    virtual int Sockfd() = 0;
    virtual void Close() = 0;

    virtual ssize_t Recv(std::string *out) = 0;
    virtual ssize_t Send(const std::string &in) = 0;

public:
    void BuildListenSocket(uint16_t port)
    {
        CreateSocketOrDie();
        CreateBindOrDie(port);
        CreateListenOrDie();
    }

    bool BuildClientSocket(const std::string &peerip, const uint16_t peerport)
    {
        CreateSocketOrDie();
        return Connect(peerip, peerport);
    }
};

class TcpSocket : public Socket
{
public:
    TcpSocket(int sockfd = -1) : _sockfd(sockfd)
    {
    }
    virtual void CreateSocketOrDie() override
    {
        _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
        if(_sockfd<0)
        {
            LOG(FATAL, "SOCK ERROR\n");
            exit(SOCKET_ERROR);
        }
        LOG(DEBUG, "SOCKET success!\n");
    }

    virtual int CreateBindOrDie(uint16_t port) override
    {
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_addr.s_addr = INADDR_ANY;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        bool ret = ::bind(_sockfd, (struct sockaddr *)&addr, sizeof(addr));
        if(ret < 0) 
        {
            LOG(FATAL, "bind error\n");
            exit(0);
        }
        LOG(FATAL, "bind success\n");
        return ret;
    }

    virtual void CreateListenOrDie(int backlog = gblacklog) override
    {
        ::listen(_sockfd, backlog);
    }

    virtual int Accept(InetAddr *cliaddr) override
    {
        struct sockaddr_in client;
        socklen_t len = sizeof(client);
        int sockfd = ::accept(_sockfd, (struct sockaddr *)&client, &len);
        *cliaddr = InetAddr(client);
        // return std::make_shared<TcpSocket>(sockfd);
        return sockfd;
    }

    virtual bool Connect(const std::string &peerip, const uint16_t peerport) override
    {
        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        ::inet_pton(AF_INET, peerip.c_str(), &addr.sin_addr);
        addr.sin_port = htons(peerport);
        return connect(_sockfd, (struct sockaddr *)&addr, sizeof(addr))>0;
    }

    virtual int Sockfd() override
    {
        return _sockfd;
    }

    virtual void Close() override
    {
        if (_sockfd > 0)
        {
            ::close(_sockfd);
        }
    }

    virtual ssize_t Recv(std::string *out) override
    {
        char inbuffer[4096];
        ssize_t n = recv(_sockfd, inbuffer, sizeof(inbuffer) - 1, 0);
        if (n > 0)
        {
            inbuffer[n] = 0;
            *out += inbuffer;
        }
        return n;
    }

    virtual ssize_t Send(const std::string &in) override
    {
        return ::send(_sockfd, in.c_str(), in.size(), 0);
    }

private:
    int _sockfd;
};
